import { _decorator, Component, instantiate, Label, Node, tween, UIOpacity } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('TurkishTetrisTipPanel')
export default class TurkishTetris_TipPanel extends Component {
    static Instance: TurkishTetris_TipPanel;
    @property(Node)
    parent: Node | null = null;
    @property(Node)
    Prefab: Node | null = null;
    array: Node[] = new Array();
    onLoad() {
        TurkishTetris_TipPanel.Instance = this;
    }
    start() { }
    Show(content: string, duration = 0.6) {
        const node: Node = instantiate(this.Prefab);
        let label = node.getComponentInChildren(Label);
        if (!label) {
            label = node.addComponent(Label);
        }

        this.DestroyAll();
        this.array.push(node);

        label.string = content;
        node.parent = this.parent;
        node.setPosition(0, 100);
        node.active = true;

        node.getComponent(UIOpacity).opacity = 0;
        tween(node.getComponent(UIOpacity))
            .to(0.6, { opacity: 255 })
            .delay(duration)
            .to(1, { opacity: 0 })
            .call(() => {
                this.DestroyAll();
            })
            .start();
    }
    DestroyAll() {
        this.array.forEach((element) => {
            element.destroy();
        });
        this.array = new Array();
    }
}

// /** 调试 */
window["TurkishTetris_TipPanel"] = TurkishTetris_TipPanel;